﻿using System;
using Systemathics.FrameWork.Interfaces;

namespace Systemathics.Providers
{
    public sealed partial class IB : IFees
    {
        #region IFees Members

        public string Broker                        { get; set; }
        public bool isPercentageFees                { get; set; }
        public double MinExecFee                    { get; set; }
        public double ExecFee                       { get; set; }
        public double ModifFee                      { get; set; }
        public double CancelFee                     { get; set; }
        public double IntradayInitialMargin         { get; set; }
        public double IntradayMaintenanceMargin     { get; set; }
        public double OvernightInitialMargin        { get; set; }
        public double OvernightMaintenanceMargin    { get; set; }
        public double Borrow                        { get; set; }
        public double Lending                       { get; set; }
        public string Crncy                         { get; set; }
        public double ReferencePrice                { get; set; }
        public double Tc                            { get; set; }
        public double TcInventory                   { get; set; }
        public double InitialMargin                 { get; set; }

        public double Execution(int qty, decimal price)
        {
            var temp = 0.0;
            if (isPercentageFees)
                temp = ExecFee * qty * Decimal.ToDouble(price) * ReferencePrice;
            else
                temp = ExecFee * qty * ReferencePrice;

            // if future or forex
            InitialMargin += qty * IntradayInitialMargin * ReferencePrice; // 2 * 800$ ou 2500 

            // if equity
            // InitialMargin += qty * Decimal.ToDouble(price) given that maximum is buying power

            if (MinExecFee > 0 && temp < MinExecFee)
                return Tc += MinExecFee;
            return Tc += temp;
        }
        public double ExecutionInventory(int qty, decimal price)
        {
            var temp = 0.0;
            if (isPercentageFees)
                temp = ExecFee * qty * Decimal.ToDouble(price) * ReferencePrice;
            else
                temp = ExecFee * qty * ReferencePrice;

            if (MinExecFee > 0 && temp < MinExecFee)
                return TcInventory += MinExecFee;
            return TcInventory += temp;
        }
        public double Modification(int qty, decimal price)
        {
            if (isPercentageFees)
                return Tc += ModifFee * qty * Decimal.ToDouble(price) * ReferencePrice;
            return Tc += ModifFee * qty * ReferencePrice;
        }
        public double Cancellation(int qty, decimal price)
        {
            if (isPercentageFees)
                return Tc += CancelFee * qty * Decimal.ToDouble(price) * ReferencePrice;
            return Tc += CancelFee * qty * ReferencePrice;
        }

        #endregion
    }
}
